const app = getApp();
const companyName = app.globalData.companyName;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    memberList: [],
    page: 1,
    pageSize: 20
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.fatchMemberList();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    const adminLogin = app.globalData.adminLogin;
    if (adminLogin === false) {
      wx.hideNavigationBarLoading();
      wx.stopPullDownRefresh();
      wx.navigateTo({
        url: '/pages/login/login',
      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  fatchMemberList: function() {
    const that = this;
    const memberList = that.data.memberList;
    const url = app.globalData.url;
    const page = that.data.page;
    const pageSize = that.data.pageSize;
    wx.showNavigationBarLoading();
    wx.request({
      url: url + '/management/queryMember',
      data: {
        companyName,
        page,
        pageSize
      },
      method: 'POST',
      success: function(response) {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        if (response.data.success) {
          const content = response.data.content;
          if (content && content.length > 0) {
            content.forEach(function(item) {
              if (item.sex = 'MAN') {
                item.sex = '先生';
              } else {
                item.sex = '小姐';
              }
              memberList.push(item);
            });
            that.setData({
              memberList
            });
          } else {
            wx.showToast({
              title: '没有更多数据了~',
              icon: 'none'
            })
          }
        } else {
          wx.showToast({
            title: response.data.message,
            icon: 'none'
          })
        }
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.setData({
      page: 1,
      pageSize: 20,
      memberList: []
    });
    this.fatchMemberList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    const page = this.data.page;
    this.setData({
      page: page + 1,
      pageSize: 20
    });
    this.fatchMemberList();
  },
  addMember: function() {
    wx.navigateTo({
      url: '/pages/addMember/addMember',
    })
  },
  deleteMember: function(e) {
    const that = this;
    const url = app.globalData.url;
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定删除此会员？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: url + '/management/deleteMember/' + id,
            success: function(response) {
              if (response.data.success) {
                that.setData({
                  page: 1,
                  pageSize: 20,
                  memberList: []
                });
                that.fatchMemberList();
              } else {
                wx.showToast({
                  title: '删除失败',
                  icon: 'none'
                });
              }
            }
          })
        }
      }
    })
  }
})